name: CI/CD

on:
  push:
    tags:
      - v*

  pull_request:
    branches:
      - canary
    paths:
      - .github/workflows/*
      - src/*

jobs:
  build:
    name: Build (${{ matrix.platform }})
    runs-on: ${{ matrix.os }}

    # Strategy
    # (variables for multiple platforms)
    # https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idstrategy
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - windows-latest
          - macOS-latest

        include:
          - os: ubuntu-latest # Ubuntu
            platform: ubuntu
            extension: snap
          - os: windows-latest # Windows
            platform: windows
            extension: exe
          - os: macOS-latest # macOS
            platform: macos
            extension: dmg

    steps:
      - name: Dump GitHub context
        if: false
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
        continue-on-error: true

      - name: Dump runner context
        if: false
        env:
          RUNNER_CONTEXT: ${{ toJson(runner) }}
        run: echo "$RUNNER_CONTEXT"
        continue-on-error: true

      - uses: actions/checkout@v1
        with:
          submodules: true

      - name: Use Node.js 12.x
        uses: actions/setup-node@v1
        with:
          node-version: 12.x

      - name: Use Yarn
        run: npm install -g yarn

      - name: Setup
        working-directory: src
        run: yarn install

      - name: Lint
        working-directory: src
        run: yarn run lint

      - name: Build (Installer)
        working-directory: src
        env:
          GITHUB_SHA: ${{ github.sha }}
        run: yarn run build

      - name: Build (Portable)
        working-directory: src
        env:
          BUILD_PORTABLE: true
        run: yarn run build

      - name: Release
        working-directory: src/scripts
        continue-on-error: true
        env:
          SECRET_KEY: ${{ secrets.SECRET_KEY }}
          GITHUB_TOKEN: ${{ secrets.TOKEN }}
          DREAMLINK_TOKEN: ${{ secrets.DREAMLINK_TOKEN }}
          PINATA_KEY: ${{ secrets.PINATA_KEY }}
          PINATA_SECRET: ${{ secrets.PINATA_SECRET }}
          CODEBERG_TOKEN: ${{ secrets.CODEBERG_TOKEN }}
          ANTOPIE_TOKEN: ${{ secrets.ANTOPIE_TOKEN }}
          SPIP_TOKEN: ${{ secrets.SPIP_TOKEN }}
          TEKNIK_TOKEN: ${{ secrets.TEKNIK_TOKEN }}
          GITHUB_SHA: ${{ github.sha }}
          GITHUB_REF: ${{ github.ref }}
          BUILD_PLATFORM: ${{ matrix.platform }}
          BUILD_EXTENSION: ${{ matrix.extension }}
        run: node release.js
